package com.mqttinsight.ui.form.panel;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import com.mqttinsight.exception.VerificationException;
import com.mqttinsight.mqtt.SecureSetting;
import com.mqttinsight.util.LangUtil;
import com.mqttinsight.util.Validator;

import javax.swing.*;

/**
 * @author ptma
 */
public class ServerKeystorePanel extends SecurePanel {
    private JPanel rootPanel;
    private JTextField fileField;
    private JPasswordField passwordField;
    private JLabel caTrustFileLabel;
    private JLabel caTrustPwdLabel;

    public ServerKeystorePanel() {
        super();
        $$$setupUI$$$();
        initSecureFileBrowserButton(fileField);
        applyLanguage();
    }

    @Override
    public void applyLanguage() {
        caTrustFileLabel.setText(LangUtil.getString("CaKeystoreFile"));
        caTrustPwdLabel.setText(LangUtil.getString("CaKeystorePassword"));
    }

    @Override
    public void verifyFields() throws VerificationException {
        Validator.notEmpty(fileField, () -> LangUtil.format("FieldRequiredValidation", caTrustFileLabel.getText()));
        Validator.notEmpty(passwordField, () -> LangUtil.format("FieldRequiredValidation", caTrustPwdLabel.getText()));
    }

    @Override
    public JPanel getRootPanel() {
        return rootPanel;
    }

    @Override
    public void changeFieldsEnable(boolean enabled) {
        fileField.setEnabled(enabled);
        passwordField.setEnabled(enabled);
    }

    @Override
    public void applySetting(SecureSetting setting) {
        if (setting != null) {
            fileField.setText(setting.getServerKeyStoreFile());
            passwordField.setText(setting.getServerKeyStorePassword());
        }
    }

    @Override
    public void resetFields() {
        fileField.setText("");
        passwordField.setText("");
    }

    @Override
    public SecureSetting getSetting() throws VerificationException {
        SecureSetting setting = new SecureSetting();
        setting.setServerKeyStoreFile(fileField.getText());
        setting.setServerKeyStorePassword(String.valueOf(passwordField.getPassword()));
        return setting;
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        rootPanel = new JPanel();
        rootPanel.setLayout(new FormLayout("fill:170px:noGrow,left:4dlu:noGrow,fill:d:grow", "center:d:noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow"));
        caTrustFileLabel = new JLabel();
        caTrustFileLabel.setText("CA keystore file");
        CellConstraints cc = new CellConstraints();
        rootPanel.add(caTrustFileLabel, cc.xy(1, 1));
        fileField = new JTextField();
        rootPanel.add(fileField, cc.xy(3, 1, CellConstraints.FILL, CellConstraints.DEFAULT));
        caTrustPwdLabel = new JLabel();
        caTrustPwdLabel.setText("CA keystore password");
        rootPanel.add(caTrustPwdLabel, cc.xy(1, 3));
        passwordField = new JPasswordField();
        rootPanel.add(passwordField, cc.xy(3, 3, CellConstraints.FILL, CellConstraints.DEFAULT));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return rootPanel;
    }

}
